<?php

namespace App\Http\Controllers\Site\Custom\Provider;

use App\B2BProject\Area;
use App\B2BProject\Custom\Business\LicensedGood;
use App\B2BProject\Site\HelperControllerBuildTrait;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class DistributionSystemRelationshipController extends Controller
{
    use HelperControllerBuildTrait;

    public function index(Request $request)
    {
        // 分页处理
        $request->query->set('page', $request->query('page', $request->page));
        $request->query->set('pageSize', $request->query('pageSize', $request->pageSize));

        if ($request->ajax()) {
            // 获取授权表数据集合
            $authorizations = \Auth::user()->providerAuthorizationRequests()->lists('id');

            $licensedGoods = LicensedGood::whereIn('authorization_id',
                $authorizations);

            if (in_array($status = $request->query('status'), ['0', '1'])) {
                $licensedGoods->where('status', (boolean)$status);
            }

            switch ($request->query('type')) {
                case '10001':
                    $licensedGoods->whereHas('goods', function($query) use ($request) {
                        $name = $request->query('key');
                        $query->where('name', 'like', "%$name%");
                    });
                    break;
                case '10002':

            }

            $licensedGoods
                = $licensedGoods->with('goods')->with('authorizationRequest')->with('priceStrategy')->paginate($request->query('pageSize'));

            $data = [];

            foreach ($licensedGoods as $item) {
                $data[] = [
                    'id'            => $item->id,
                    'img'           => $item->goods->cover(),
                    'proname'       => $item->goods->name,
                    'tradeprice'    => $item->goods->original_price,
                    'purchaseprice' => sprintf("%01.2f",
                        $item->goods->original_price * $item->priceStrategy->price_strategies),
                    'buyer'         => $item->authorizationRequest->company_name,
                    'rangearea'     => Area::getName($item->province_id) . ' ' . Area::getName($item->city_id),
                    'buyerrange'    => ['未授权', '合作中', '过期'][$item->authorizationRequest->getAuthorizationStatus()],
                    'href_url'      => route('account.my-purchaser.licensed',
                        [$item->authorizationRequest->id]),
                ];
            }

            return response()->json([
                'data'           => $data,
                'pagingConfData' => [
                    'page'     => $licensedGoods->currentPage(),
                    'pageSize' => $request->query('pageSize'),
                    'total'    => $licensedGoods->total(),
                ],
            ]);
        }

        return view('host/custom/provider/distribution-system-relationship/index');
    }
}
